//
//  WXTabBar.m
//  weibo-OC
//
//  Created by 王旭 on 16/5/19.
//  Copyright © 2016年 王旭. All rights reserved.
//

#import "WXTabBar.h"
#import "UIView+XYWH.h"

@interface WXTabBar()

@property (nonatomic,strong)UIButton *addButton;

@end

@implementation WXTabBar

- (instancetype)init{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat buttonW = self.bounds.size.width / 5;
    
    //调整其他按钮位置和大小
    int i = 0;
    
    for (UIView *value in self.subviews) {
        if ([value isKindOfClass:NSClassFromString(@"UITabBarButton")]) {

            //宽度
            value.w = buttonW;
            
            //位置
            value.x = i * buttonW;
            i++;
            
            if (i == 2) {
                i++;
            }
            
        }
    }

    
    
    
    //调整自定义按钮位置和大小
    self.addButton.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    
    self.addButton.w = buttonW;
    
}

#pragma mark -
#pragma mark 方法调用

/**
 *  布局界面
 */
- (void)setupUI {
    [self addSubview:self.addButton];
}

/**
 *  添加按钮点击
 */
- (void)addButtonDidClick {
    //执行block
    if (self.addButtonBlock) {
        self.addButtonBlock();
    }
}


#pragma mark -
#pragma mark 懒加载
- (UIButton *)addButton{
    if (_addButton == nil) {
        
        UIButton *button = [[UIButton alloc]init];
        
        [button addTarget:self action:@selector(addButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
        
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [button sizeToFit];
        
        _addButton = button;
    }
    return _addButton;
}

@end
